<?php

class cCache {
	
	public $use = false;
	
	function __construct($cms) 
	{
		$this->cms = $cms;
	}
	
	function cachedResult(cView $view) 
	{
		if (!$this->use) return;
		
		$r = $this->cms->tree->query('SELECT * FROM `core_cache` WHERE id ='.$view->id.' AND `view` =\''.serialize($view->getParams()).'\'');	
		
		if ($cached = mysql_fetch_array($r)) 
		{
			return $cached['page'];			
		} else return false;
	}
	
	function cachePage($page, cView $view) 
	{
		if (!$this->use) return;
		
		$dependsOn = '|'.implode('|',array_keys($this->cms->tree->usedIds)).'|';
		$this->cms->tree->query('INSERT `core_cache` SET `id`='.$view->id.', `view` =\''.serialize($view->getParams()).'\', `created_at`=\''.date("Y-m-d H:i:s").'\', page=\''.mysql_real_escape_string($page).'\', depends_on=\''.$dependsOn.'\'');
	}
	
	function changed($id) 
	{
		if (is_numeric($id))
		$this->cms->tree->query('DELETE FROM `core_cache` WHERE id ='.$id.' OR `depends_on` LIKE \'%|'.$id.'|%\' ');			
	}
	
}

?>